package com.card.client.view;

import java.util.ArrayList;
import java.util.Date;

import com.card.client.entry.CreditCardBill;
import com.card.client.entry.ViewObject;
import com.card.client.service.CreditCardBillService;
import com.card.client.service.CreditCardBillServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TabPanel;

public class CreditCardBillView extends Composite implements ViewObject {

//	private final CreditCardServiceAsync ccs = GWT.create(CreditCardService.class);
	
	private final CreditCardBillServiceAsync ccbs = GWT.create(CreditCardBillService.class);
	
	private TabPanel tabPanel = new TabPanel();
	
	private String[] financeMonth;
	
	CreditCardBillView creditCardBillView;

	public CreditCardBillView() {
		init();
		initWidget(tabPanel);
		creditCardBillView = this;
		setTabPanel();
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
			public void onSelection(SelectionEvent<Integer> event) {
				System.out.println("aa");
			}
		});
	}

	private void init(){
		tabPanel.setWidth("100%");
		tabPanel.setHeight("100%");
		tabPanel.getDeckPanel().setHeight("100%");
		tabPanel.getDeckPanel().getParent().setHeight("100%");
	}
	
	private void setTabPanel() {
		init();
		ccbs.getAllCreditCardBill(new AsyncCallback<CreditCardBill[]>() {
			public void onSuccess(CreditCardBill[] result) {
				ArrayList<String> temp = new ArrayList<String>();
				if(result.length>0){
					for(int i=0;i<result.length;i++){
						CreditCardBill ccb = result[i];
						boolean add = true;
						if(temp.isEmpty()){
							temp.add(ccb.getFinanceMonth());
							add = false;
						} else {
							for(int a=0;a<temp.size();a++){
								String fm = temp.get(a);
								if(fm.equals(ccb.getFinanceMonth())){
									add = false;
									break;
								}
							}
						}
						if(add){
							temp.add(ccb.getFinanceMonth());
						}
					}
					financeMonth = new String[temp.size()];
					for(int i=0;i<financeMonth.length;i++){
						financeMonth[i] = temp.get(i);
					}
				} else {
					financeMonth = new String[1];
					DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM");
					String thisMonth = dateTimeFormat.format(new Date());
					financeMonth[0] = thisMonth;
				}
				int count = tabPanel.getWidgetCount();
				for(int i=0;i<financeMonth.length;i++){
					boolean add = true;
//					CreditCardBillInfoView ccbiv = new CreditCardBillInfoView(financeMonth[i], creditCardBillView);
					for(int a=0;a<count;a++){
//						CreditCardBillInfoView w = (CreditCardBillInfoView)tabPanel.getWidget(a);
//						String mon = w.getFinanceMonth();
//						if(mon.equals(financeMonth[i])){
//							add = false;
//							break;
//						}
					}
					if(add){
//						tabPanel.add(ccbiv, financeMonth[i]);
					}
				}
				tabPanel.selectTab(0);
			}
			public void onFailure(Throwable caught) {}
		});
	}

	public void onLoad(){
		setTabPanel();
	}

	@Override
	public void delDate(Long id) {
		if (id == null) {
			return;
		}
		ccbs.delCreditCardBill(id, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				onLoad();
			}

			public void onFailure(Throwable caught) {
			}
		});
	}
}
